package com.zyb.warehouse.controller;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.*;
import com.zyb.warehouse.service.*;
import com.zyb.warehouse.utils.WarehouseConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Description:商品列表控制层
 */
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private StoreService storeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeService typeService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private UnitService unitService;

    /**
     * 查询所有仓库
     *
     * @return
     */
    @GetMapping("/store-list")
    public Result queryAllStore() {
        List<Store> storeList = storeService.queryAllStore();
        return Result.ok(storeList);
    }

    /**
     * 查询所有品牌
     *
     * @return
     */
    @GetMapping("/brand-list")
    public Result queryAllBrand() {
        List<Brand> brandList = brandService.queryAllBrand();
        return Result.ok(brandList);
    }

    /**
     * 分页查询商品
     *
     * @param page
     * @param product
     * @return
     */
    @GetMapping("/product-page-list")
    public Result page(Page page, Product product) {
        Page pages = productService.pageProduct(page, product);
        return Result.ok(pages);
    }

    /**
     * 查询商品分类
     *
     * @return
     */
    @GetMapping("/category-tree")
    public Result queryCategoryTree() {
        List<ProductType> productTypeList = typeService.queryCategoryTree();
        return Result.ok(productTypeList);
    }

    /**
     * 查询所有供应商
     *
     * @return
     */
    @GetMapping("/supply-list")
    public Result queryAllSupply() {
        List<Supply> supplyList = supplyService.queryAllSupply();
        return Result.ok(supplyList);
    }

    /**
     * 查询所有产地
     *
     * @return
     */
    @GetMapping("/place-list")
    public Result queryAllPlace() {
        List<Place> placeList = placeService.queryAllPlace();
        return Result.ok(placeList);
    }

    /**
     * 查询所有产地
     *
     * @return
     */
    @GetMapping("/unit-list")
    public Result queryAllUnit() {
        List<Unit> unitList = unitService.queryAllUnit();
        return Result.ok(unitList);
    }

    /**
     * 图片上传
     * 将配置文件的file.upload-path属性值注入给控制器的uploadPath属性,
     * 其为图片上传到项目的目录路径(类路径classes即resources下的static/img/upload);
     *
     * @param file
     * @return
     */
    @Value("${file.upload-path}")
    private String uploadPath;

    @CrossOrigin
    @PostMapping("/img-upload")
    public Result upload(MultipartFile file) {
        try {
            //拿到图片上传到的目录(类路径classes下的static/img/upload)的File对象
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到的目录的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到图片保存到的磁盘路径
            String fileUploadPath = uploadDirPath + "\\" + file.getOriginalFilename();
            //保存图片
            file.transferTo(new File(fileUploadPath));
            //成功响应
            return Result.ok("图片上传成功！");
        } catch (IOException e) {
            //失败响应
            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }

    /**
     * 添加商品
     *
     * @param product
     * @param token
     * @return
     */
    @PostMapping("/product-add")
    public Result addProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = productService.addProduct(product, token);
        return result;
    }

    /**
     * 修改商品的上下架状态
     *
     * @param product
     * @param token
     * @return
     */
    @PutMapping("/state-change")
    public Result stateChange(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = productService.changeProductState(product, token);
        return result;
    }

    /**
     * 删除单个商品
     *
     * @param productId
     * @return
     */
    @DeleteMapping("/product-delete/{productId}")
    public Result removeOne(@PathVariable("productId") Integer productId) {
        List<Integer> productIds = Arrays.asList(productId);
        Result result = productService.removeProduct(productIds);
        return result;
    }

    /**
     * 批量删除商品
     *
     * @param productIds
     * @return
     */
    @DeleteMapping("/product-list-delete")
    public Result beachRemove(@RequestBody List<Integer> productIds) {
        Result result = productService.removeProduct(productIds);
        return result;
    }

    /**
     * 修改商品
     *
     * @param product
     * @param token
     * @return
     */
    @PutMapping("/product-update")
    public Result modifyProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = productService.modifyProduct(product, token);
        return result;
    }
}
